package com.zhuoshi.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhuoshi.bean.PaymentBean;
import com.zhuoshi.bean.PaymentGroupBean;
import com.zhuoshi.utils.DbHelper;

/**
 * 回款管理表 数据处理层
 * 
 * @author Administrator
 *
 */
public class PaymentDao {
	// 添加
	public int add(PaymentBean bean) {
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("insert into payment (numerical,paydate,paymoney) values");
			sql.append(" (?,?,?)");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, bean.getNumerical());
			pstat.setDate(2, bean.getPaydate());
			pstat.setDouble(3, bean.getPaymoney());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, null);
		}

		return rows;
	}

	// 修改
	public int update(PaymentBean bean) {
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("update payment set ");
			sql.append("numerical=?,");
			sql.append("paydate=?,");
			sql.append("paymoney=?");
			
			sql.append(" where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, bean.getNumerical());
			pstat.setDate(2, bean.getPaydate());
			pstat.setDouble(3, bean.getPaymoney());
			pstat.setInt(4, bean.getId());
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, null);
		}

		return rows;
	}

	// 删除
	public int delete(int id) {
		Connection conn = null;
		PreparedStatement pstat = null;
		int rows = 0;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("delete from payment where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rows = pstat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, null);
		}

		return rows;
	}

	/**
	 * 通过id查询 获取一个数据实体
	 * 
	 * @param id
	 *            自增ID
	 * @return
	 */
	public PaymentBean getModel(int id) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		PaymentBean bean = null;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select * from payment where id=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while (rs.next()) {
				bean = new PaymentBean();
				bean.setId(rs.getInt("id"));
				bean.setNumerical(rs.getInt("numerical"));
				bean.setPaydate(rs.getDate("paydate"));
				bean.setPaymoney(rs.getDouble("paymoney"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return bean;
	}

	/**
	 * 查询多条 List
	 * 
	 * @param bean
	 *            条件 如果没有条件 传入null 查询所有
	 * @return
	 */
	public List<PaymentBean> getList(PaymentBean bean, String startDate, String endDate) {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<PaymentBean> list = new ArrayList<PaymentBean>();
		try {
			conn = DbHelper.getConnection();
			// ---===================构建查询条件=================---//
			StringBuffer sql = new StringBuffer();
			sql.append("select * from payment where 1=1");
			// if(bean!=null){

			if (null != bean.getNumerical()) {// 合同数字编号
				sql.append(" and numerical like '%" + bean.getNumerical() + "%'");
			}
			if (!"".equals(startDate) && startDate != null) {
				sql.append(" and paydate>='" + startDate + "'");
			}

			if (!"".equals(endDate) && endDate != null) {
				sql.append(" and paydate<='" + endDate + "'");
			}

			// }
			// ---===================/构建查询条件=================---//
			pstat = conn.prepareStatement(sql.toString());
			// System.out.println(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				PaymentBean cBean = new PaymentBean();
				cBean.setId(rs.getInt("id"));
				cBean.setNumerical(rs.getInt("numerical"));
				cBean.setPaydate(rs.getDate("paydate"));
				cBean.setPaymoney(rs.getDouble("paymoney"));
				list.add(cBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}
	
	public List<PaymentBean> getAllList() {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<PaymentBean> list = new ArrayList<PaymentBean>();
		try {
			conn = DbHelper.getConnection();
			// ---===================构建查询条件=================---//
			StringBuffer sql = new StringBuffer();
			sql.append("select * from payment where 1=1");
			
			// ---===================/构建查询条件=================---//
			pstat = conn.prepareStatement(sql.toString());
			// System.out.println(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				PaymentBean cBean = new PaymentBean();
				cBean.setId(rs.getInt("id"));
				cBean.setNumerical(rs.getInt("numerical"));
				cBean.setPaydate(rs.getDate("paydate"));
				cBean.setPaymoney(rs.getDouble("paymoney"));
				list.add(cBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}
	
	//取合同数字编号  distinct
	public List<PaymentBean> getAllNumerical() {
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		List<PaymentBean> list = new ArrayList<PaymentBean>();
		try {
			conn = DbHelper.getConnection();
			// ---===================构建查询条件=================---//
			StringBuffer sql = new StringBuffer();
			sql.append("select DISTINCT(numerical) from payment where 1=1");
			
			// ---===================/构建查询条件=================---//
			pstat = conn.prepareStatement(sql.toString());
			// System.out.println(sql);
			rs = pstat.executeQuery();
			while (rs.next()) {
				PaymentBean cBean = new PaymentBean();
				cBean.setNumerical(rs.getInt("numerical"));
				list.add(cBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbHelper.dispose(conn, pstat, rs);
		}
		return list;
	}
	
	
	public List<PaymentGroupBean> getGroupList(){
		List<PaymentBean> paymentList = getAllNumerical();
		List<PaymentGroupBean> list = new ArrayList<PaymentGroupBean>();
		int i=0;
		for (PaymentBean bean : paymentList) {
			i = i+1;
			PaymentGroupBean pgBean = new PaymentGroupBean();
			pgBean.setId(i);
			pgBean.setNumerical(bean.getNumerical());
			double totalpaymoney = getAllPayment(bean.getNumerical());
			pgBean.setTotalPaymoney(totalpaymoney);
			double contractMoney = new ContractSubDao().getTotalMoney(bean.getNumerical());//合同总金额
			double d = totalpaymoney/contractMoney;
			String payState = "";
			//System.out.println(d);
			if(d==1){
				payState="已回完";
			}else{
				payState = "回款中";
			}
			pgBean.setPayState(payState);
			pgBean.setPayPercent(d);
			list.add(pgBean);
		}
		
		
		return list;
	}
	
	public double getAllPayment(int numerical){
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		double result = 0d;
		try {
			conn = DbHelper.getConnection();
			StringBuffer sql = new StringBuffer();
			sql.append("select sum(paymoney) from payment where 1=1");
			sql.append(" and numerical=?");
			pstat = conn.prepareStatement(sql.toString());
			pstat.setInt(1, numerical);
			rs = pstat.executeQuery();
			while(rs.next()){
				result = rs.getDouble(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DbHelper.dispose(conn, pstat, rs);
		}
		return result;
	}
	
}
